from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
          #文本
        #1文本
        text5 = Tex(
                    r"\text{二元极限攻略}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-39,20,0])
        #2框框标
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=18,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([-39,20,0])
        #3箭头
        arrow1 = Arrow(start=[-29,23,0],end=[-25,27.5,0], color=RED
                       , stroke_width=20, tip_length=1)
        arrow2 = Arrow(start=[-29,20,0],end=[-25,20,0], color=RED
                       , stroke_width=20, tip_length=1)
        arrow3 = Arrow(start=[-29,18,0],end=[-25,12.5,0], color=RED
                       , stroke_width=20, tip_length=1)
        #4文本
        text6 = Tex(
                    r"\text{四则运算法则,保号性...(略)}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-8,27.5,0])
        text7 = Tex(
                    r"\text{计算}$\star$",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,20,0])
        text8 = Tex(
                    r"\text{应用领域}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,12.5,0])
        #5箭头
       
        arrow6 = Arrow(start=[-15,21,0],end=[-10,22,0], color=RED
                       , stroke_width=20, tip_length=1)
        arrow7 = Arrow(start=[-15,19,0],end=[-10,18,0], color=RED
                       , stroke_width=20, tip_length=1)
        arrow8 = Arrow(start=[-13,12.5,0],end=[-5,12.5,0], color=RED
                       , stroke_width=20, tip_length=1)
       
        text11 = Tex(
                    r"\text{极限存在}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,22,0])
        text12 = Tex(
                    r"\text{极限不存在}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,18,0])
        text13 = Tex(
                    r"\text{某点}$\left( x_{0},y_{0}\right) $\text{是否连续,可偏导,可微}$?$",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([16,12.5,0])
        self.play(Write(text5),Create(rectangle1))
        self.play(Create(arrow1),Create(arrow2),Create(arrow3))
        self.play(Create(text6),Create(text7),Create(text8))
        self.play(Create(arrow6),Create(arrow7),Create(arrow8))
        self.play(Create(text11),Create(text12),Create(text13))
        #
        self.play(
            
            text7.animate.set_color(YELLOW),
            text11.animate.set_color(BLUE),
            text12.animate.set_color(BLUE)       # 改变颜色动画
        )
        text14 = Tex(
                    r"\text{(高阶/低阶)}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([13,22,0])
        text15 = Tex(
                    r"\text{(低阶/高阶)}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([13,18,0])
        self.play(Write(text15),Write(text14))

        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=10
                            ,stroke_color=RED,stroke_width=20)
        rectangle2.move_to([13,20,0])
        self.play(Create(rectangle2))
        text18 = Tex(
                    r"\text{观察}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([13,27.5,0])
        self.play(Write(text18))
        text16 = Tex(
                    r"\text{(无穷小}$x$\text{有界/夹逼)}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,22,0])
        text17 = Tex(
                    r"\text{(选取任意路径}$y=kx$\text{)}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,18,0])
        self.play(Write(text16),Write(text17))
        rectangle3=Rectangle(color=BLUE,fill_opacity=0.0,width=27,height=10
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle3.move_to([35,20,0])
        self.play(Create(rectangle3))
        text19 = Tex(
                    r"\text{证明}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,27.5,0])
        self.play(Write(text19))

        text20 = Tex(
                    r"\text{检验函数}$f\left( x,y\right) =\begin{cases}\dfrac{x^{3}+y^{3}}{x^{2}+y^{2}},\left( x,y\right) \neq \left( 0,0\right) \\0,\left( x,y\right) =\left( 0,0\right) \end{cases}$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-23,4,0])
        text21 = Tex(
                    r"\text{在}$\left( 0,0\right) $\text{处的连续性,偏导性,可微性}",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,4,0])
        self.play(Write(text20),Write(text21))

        text22 = Tex(
                    r"\text{解：}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,-5,0])
        self.play(Write(text22))

        text23 = Tex(
                    r"$\lim\limits _{\left( x,y\right) \rightarrow \left( 0,0\right) }f\left( x,y\right) =\lim\limits _{\left( x,y\right) \rightarrow \left( 0,0\right) }\dfrac{x^{3}+y^{3}}{x^{2}+y^{2}}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-7,0])
        self.play(Write(text23))
        text24 = Tex(
                    r"$=0=f\left( 0,0\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-7,0])
        self.play(Write(text24))
        text25 = Tex(
                    r"\text{(连续)}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-7,0])
        self.play(Write(text25))


        text26 = Tex(
                    r"$f_{x}'\left( 0,0\right) =\lim\limits _{x\rightarrow 0}\dfrac{f\left( x,0\right) -f\left( 0,0\right) }{x}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-14,0])
        self.play(Write(text26))
        text27 = Tex(
                    r"$=\lim\limits _{x\rightarrow 0}\dfrac{x}{x}=1$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-14,0])
        self.play(Write(text27))
        text28 = Tex(
                    r"\text{(存在)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-14,0])
        self.play(Write(text28))

        text29 = Tex(
                    r"$f_{y}'\left( 0,0\right) =\lim\limits _{y\rightarrow 0}\dfrac{f\left( 0,y\right) -f\left( 0,0\right) }{y}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-21,0])
        self.play(Write(text29))
        text30 = Tex(
                    r"$=\lim\limits _{y\rightarrow 0}\dfrac{y}{y}=1$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-21,0])
        self.play(Write(text30))
        text31 = Tex(
                    r"\text{(存在)}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-21,0])
        self.play(Write(text31))

        self.play(Uncreate(text31),Uncreate(text30),Uncreate(text29),Uncreate(text28)
                  ,Uncreate(text27),Uncreate(text26),Uncreate(text25)
                  ,Uncreate(text24),Uncreate(text23),Uncreate(text22))

        text29 = Tex(
                    r"$\lim\limits _{\left( x,y\right) \rightarrow \left( 0,0\right) }\dfrac{f\left( x,y\right) -f\left( 0,0\right) -f_{x}\left( 0,0\right) x-f_{y}\left( 0,0\right) y}{\sqrt{x^{2}+y^{2}}}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,-7,0])
        self.play(Write(text29))
        text30 = Tex(
                    r"$=\lim\limits _{\left( x,y\right) \rightarrow \left( 0,0\right) }\dfrac{\dfrac{x^{3}+y^{3}}{x^{2}+y^{2}}-0-x-y}{\sqrt{x^{2}+y^{2}}}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-7,0])
        self.play(Write(text30))
        text32 = Tex(
                    r"$=\lim\limits _{\left( x,y\right) \rightarrow \left( 0,0\right) }\dfrac{-x^{2}y-y^{2}x}{\left( x^{2}+y^{2}\right) ^{3/2}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([23,-19,0])
        self.play(Write(text32))
        text31 = Tex(
                    r"\text{(不存在)}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([43,-19,0])
        self.play(Write(text31))
        text310 = Tex(
                    r"\text{(不可微)}",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([43,-19,0])
        self.play(ReplacementTransform(text31,text310))
        rectangle3=Rectangle(color=BLUE,fill_opacity=0.0,width=25,height=10
                            ,stroke_color=RED,stroke_width=20)
        rectangle3.move_to([25,-19,0])
        self.play(Create(rectangle3))
        text33 = Tex(
                    r"\text{取}$y=kx$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([-20,-19,0])
        self.play(Write(text33))

        text34 = Tex(
                    r"$\lim\limits _{x\rightarrow 0}\dfrac{\left( -k-k^{2}\right) x^{3}}{\left( 1+k^{2}\right) ^{\frac{3}{2}}x^{3}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,-19,0])
        self.play(
            ReplacementTransform(text33,text34),
            ReplacementTransform(rectangle3,text34)
        )
        self.wait(0.5)
        text35 = Tex(
                    r"$=\lim\limits _{x\rightarrow 0}\dfrac{\left( -k-k^{2}\right) }{\left( 1+k^{2}\right) ^{\frac{3}{2}}}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-19,0])
        self.play(Write(text35))
        text36 = Tex(
                    r"\text{(不存在)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-19,0])
        self.play(Write(text36))
        self.wait()
       